package com.mrobuy.jd.api.category;

import com.gavel.database.SQLExecutor;
import com.google.gson.Gson;
import com.jd.open.api.sdk.JdClient;
import com.jd.open.api.sdk.domain.supplier.ProductManagementService.response.find.*;
import com.jd.open.api.sdk.request.supplier.VcItemExtPropsFindRequest;
import com.jd.open.api.sdk.request.supplier.VcItemPropsFindRequest;
import com.jd.open.api.sdk.response.supplier.VcItemExtPropsFindResponse;
import com.jd.open.api.sdk.response.supplier.VcItemPropsFindResponse;
import com.mrobuy.jd.api.JosClient;
import com.mrobuy.jdvc.entity.VCCategoryExtProp;
import com.mrobuy.jdvc.entity.VCCategoryProp;
import com.mrobuy.jdvc.entity.VCExtPropValue;
import com.mrobuy.jdvc.entity.VCPropValue;
import com.mrobuy.util.MD5Utils;

import java.util.ArrayList;
import java.util.List;

/**
 *  获取我的类目列表
 *
 *  jingdong.vc.item.products.find
 *
 *  <url>https://open.jd.com/home/home#/doc/api?apiCateId=86&apiId=1681&apiName=jingdong.vc.item.products.find</url>
 *
 *
 *
 */
public class ExtPropFindClient extends JosClient<List<VCCategoryExtProp>> {

    private VcItemExtPropsFindRequest request=new VcItemExtPropsFindRequest();

    public ExtPropFindClient(JdClient client) {
        super(client);
    }

    @Override
    public List<VCCategoryExtProp> call() throws Exception {

        List<VCCategoryExtProp> result = new ArrayList<>();

        VcItemExtPropsFindResponse response= client.execute(request);
        if ( response.getJosResultDto().getIsSuccess() ) {

            if ( response.getJosResultDto().getResult()!=null ) {

                Gson gson = new Gson();

                for (ExtPropDtow extPropDtow : response.getJosResultDto().getResult()) {

                    VCCategoryExtProp vcCategoryExtProp = gson.fromJson(gson.toJson(extPropDtow), VCCategoryExtProp.class);
                    result.add(vcCategoryExtProp);
                    if (extPropDtow.getExtPropValue()!=null ) {
                        List<VCExtPropValue> extPropValues = new ArrayList<>();
                        for (ExtPropValueDto extPropValueDto : extPropDtow.getExtPropValue()) {
                            VCExtPropValue vcExtPropValue = gson.fromJson(gson.toJson(extPropValueDto), VCExtPropValue.class);
                            vcExtPropValue.setCid(vcCategoryExtProp.getCid());
                            vcExtPropValue.setPropid(vcCategoryExtProp.getAttId());
                            extPropValues.add(vcExtPropValue);
                        }
                        vcCategoryExtProp.setOptions(extPropValues);
                    }
                }
            }
        }

        return result;
    }



    public ExtPropFindClient setCid3(int cid3) {
        request.setCategoryLeafId(cid3);
        return this;
    }
}
